﻿using MiniExcelLibs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.SS.UserModel;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace StartEAS_API.Utils
{

    public class Utils
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static string GenerateMD5(string txt)
        {
            using (MD5 mi = MD5.Create())
            {
                byte[] buffer = Encoding.Default.GetBytes(txt);
                //开始加密
                byte[] newBuffer = mi.ComputeHash(buffer);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < newBuffer.Length; i++)
                {
                    sb.Append(newBuffer[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

        /// <summary>
        /// 生成验证码随机数
        /// </summary>
        /// <returns></returns>
        public static int GenerateCaptcha() {
            //创建随机数
            Random Rdm = new Random(Guid.NewGuid().GetHashCode());
            //定义一个字符串用来存放5位随机数
            string sRnd = "";
            //for循环产生4个随机数，然后构成一个字符串
            for (int i = 0; i < 7; i++)
            {
                int iNum = Rdm.Next(0, 10);
                sRnd += iNum.ToString();
            }
            return int.Parse(sRnd);
        }

        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long ConvertDateTimeToInt(System.DateTime time)
        {
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            //long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            long t = (time.Ticks - 621356256000000000) / 10000;
            return t;
        }

        public static DataSet ExcelToDS(string Path)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = null;
            strExcel = "select * from [sheet1$]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            ds = new DataSet();
            myCommand.Fill(ds, "table1");
            return ds;
        }

        public static Object ReadExcel(string path) {
            //string importExcelPath = "E:\\import.xlsx";
            //string exportExcelPath = "E:\\export.xlsx";
            //IWorkbook workbook = WorkbookFactory.Create(importExcelPath);
            IWorkbook workbook = WorkbookFactory.Create(path);
            ISheet sheet = workbook.GetSheetAt(0);//获取第一个工作薄
            //IRow row = (IRow)sheet.GetRow(0);//获取第一行
            List<Course> courseList = new List<Course>();
            for (int i = 1; i < sheet.LastRowNum; i++) { 
                IRow row = sheet.GetRow(i);
                if (row != null) {
                    Course c = new Course();
                    c.CourseId = int.Parse(row.GetCell(0).ToString());
                    c.CourseName = row.GetCell(1).ToString();
                    c.CourseAttribute = row.GetCell(2).ToString();
                    c.CourseCredits = row.GetCell(3).ToString();
                    courseList.Add(c);
                }
            }
            return courseList;
            //设置第一行第一列值,更多方法请参考源官方Demo
            //row.CreateCell(0).SetCellValue("test");//设置第一行第一列值

            //导出excel
            //FileStream fs = new FileStream(exportExcelPath, FileMode.Create, FileAccess.ReadWrite);
            //workbook.Write(fs);
            //fs.Close();
        }

        public static Object ReadSchedule(string path) { 
            var row = MiniExcel.Query<Schedule>(path).ToList();
            return row;
        }

        public async static Task<Stream> GetExcel(string path) {
            string paths = $"Projects/{path}.xlsx";
            FileStream stream = new FileStream(paths, FileMode.Open);
            return stream;
        }



    }
}
